?><?
function asHTML($str){
	return nl2br(htmlspecialchars($str));
}

function get_bool_value(&$value){return (isset($value)&&$value!="0")?1:0;}
function get_chekbox_value($value){return (isset($value)&&($value=="on"||$value==1))?1:0;}
function get_chekbox_onoff(&$value){return (isset($value))?"on":"off";}
function ret_checked(&$value){return (isset($value)&&$value!="0")?"checked":"";}

function as_email($email,$class="email",$title=""){
	?><a class="<?=$class?>" href="mailto:<?=encode_mail($email)?>"><?=$title?></a><?
}

function encode_mail($email){
	return $email;
}

function check_is_email($email) {
 return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function ismail($s){return check_is_email($s);}
function is_mail($s){return check_is_email($s);}
function is_email($s){return check_is_email($s);}

function SetXMLResponse(){
	header("Content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"windows-1251\" standalone=\"yes\"?>";
}
function xml_head(){SetXMLResponse();}


function js_and_entity_check( $html ) {
	$pattern= "/=[\S\s]*s\s*c\s*r\s*i\s*p\s*t\s*:\s*\S+/Ui";
	if (preg_match($pattern, $html)) {return true;}	
	$pattern= "/<[\S\s]*&#[x0-9]*[\S\s]*>/Ui";
	if (preg_match($pattern, $html)) {return true;}	
	return false;
}

function strip_attributes ($html, $attrs) {
	if (!is_array($attrs)) {$array= array( "$attrs" );unset($attrs);$attrs= $array;}
	foreach ($attrs AS $attribute) {
		$search[]= "/".$attribute.'\s*=\s*".+"/Uis';
		$search[]= "/".$attribute."\s*=\s*'.+'/Uis";
		$search[]= "/".$attribute."\s*=\s*\S+/i";
	}
	$html= preg_replace($search, "", $html);
	foreach ($search AS $pattern) {
		if (preg_match($pattern, $html)){$html= strip_tags($html);break;}
	}
	return $html;
}

function safe_html ($html, $allowedtags="") {
	if (js_and_entity_check($html)){$html= strip_tags($html);return $html;}
	if ($allowedtags=="") {
		$allowedtags= array ( "p"=>1, "br"=>0, "a"=>1, "img"=>0, 
			"li"=>1, "ol"=>1, "ul"=>1, 
			"b"=>1, "i"=>1, "em"=>1, "strong"=>1, 
			"del"=>1, "ins"=>1, "u"=>1, "code"=>1, "pre"=>1, 
			"blockquote"=>1, "hr"=>0
		);
	}elseif (!is_array($allowedtags)){$array= array( "$allowedtags" );}

	$stripallowed= "";
	foreach ($allowedtags AS $tag=>$closeit) {$stripallowed.= "<$tag>";}

	$html= strip_tags($html, $stripallowed);
	$badattrs= array("on\w+", "style", "fs\w+", "seek\w+");
	$html= strip_attributes($html, $badattrs);
	foreach ($allowedtags AS $tag=>$closeit) {
		if (!$closeit) continue;
		$patternopen= "/<$tag\b[^>]*>/Ui";
		$patternclose= "/<\/$tag\b[^>]*>/Ui";
		$totalopen= preg_match_all ( $patternopen, $html, $matches );
		$totalclose= preg_match_all ( $patternclose, $html, $matches2 );
		if ($totalopen>$totalclose) {
			$html.= str_repeat("</$tag>", ($totalopen - $totalclose));
		}
	}
	if ( js_and_entity_check( $html ) ) {
		$html= strip_tags($html);
		return $html;
	}
	return $html;
}


function std_css($css="styles.css",$mode=cssLinking){
	if(!is_array($css)){$css=array($css);}
	$css=gglv("predefined_styles")?array_merge($css,gglv("predefined_styles")):$css;
	switch($mode){
		case cssLinking:{
			foreach($css as $key=>$css_file){
				if((int)$key==0&&strtolower($key)!="all"&&$key!==0){?><<??>!--[if <?=$key?>]><?}
				if(file_exists($css_file)){?><link href="<?=get_host()."/".$css_file?>?<?=filemtime($css_file)?>" rel="stylesheet" type="text/css" /><?}
				if((int)$key==0&&strtolower($key)!="all"&&$key!==0){?><<??>![endif]--><?}
			}
			break;
		}
		case cssInclude:{
			foreach($css as $css_file){
				if(file_exists($css_file)){
					?><style><?=GetFileContent($css_file)?></style><?
				}
			}
			break;
		}
	}
}

function clear(){?><div class="cleaner"></div><?}

function std_js($js="scripts.js",$mode=cssLinking){
	if(!is_array($js))$js=array($js);

	switch($mode){
		case cssLinking:{
			foreach($js as $file){
				if(file_exists($file)){
					?><<??>script src="<?=get_host()."/".$file?>?<?=filemtime($file)?>" type="text/javascript" ></script><?
				}
			}
			break;
		}
		case cssInclude:{
			foreach($js as $file){
				if(file_exists($file)){
					?><<??>script type="text/javascript" ><?=GetFileContent($file)?></script><?
				}
			}
			break;
		}
	}

}

function value_list($id,$fields){
	?><div id="<?=$id?>" class="value_list"><?
	foreach($fields as $title=>$value){?><div class="value_list_item"><div class="title"><?=$title?></div><div class="value"><?=$value?></div></div><?}
	?></div><?
}
function action_list($id,$fields){
	?><div id="<?=$id?>" class="action_list"><?
	foreach($fields as $field=>$title){
		if(is_a($title,"cls_control")){
			$url=$title->url(get_current_control());
			$txt=$title->title(get_current_control());
		}else{
			$url=$field;
			$txt=$title;
		}
		?><div class="action_list_item"><a href="<?=$url?>"><?=$txt?></a></div><?
	}
	?></div><?
}

function cm($message){?><p class="cms_message"><?=$message?></p><?}
function cmtr($message){?><p class="cms_message"><?=tr($message)?></p><?}

function hide_block($name,$title,$opened=false){
	?><div id="hide_block_<?=$name?>" class="hide_block"><?
		?><div id="hide_block_<?=$name?>_title"	onClick="classSwitch(this,'hide_block_<?=$name?>_content','content_open','content_close');classSwitch(this,'hide_block_<?=$name?>_title','title_open','title_close');" class="title_<?=$opened?"open":"close"?>"><?=$title?></div><?
		?><div id="hide_block_<?=$name?>_content"	class="content_<?=$opened?"open":"close"?>"><?
}

function hide_block_close(){?></div><??></div><?}